package com.zf.mall.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.zf.mall.api.feign.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("consumer/user")
@RefreshScope
public class UserController extends BaseController {

	@Autowired
	UserService userService;

	@GetMapping("/register")
	public String register( String fix , Integer start) {
		
		JSONObject param = new JSONObject();
		
		long begin = new Date().getTime();
		for (int i = start ; i < (start + 100) ; i++) {
			param.put("userMobile", (fix + i));
	        param.put("password", "zyfss");
			HttpEntity<JSONObject> formEntity = new HttpEntity<JSONObject>(param);
	        String result = restTemplate.postForObject(ZMALLUSERSERVICE_URL+"/user/register", formEntity, String.class);
	        log.info("微服务提供方,返回的请求结果是: {} ",result);
		}
		long limit  = (new Date().getTime() -  begin) / 1000;
		
        
        
        return "请求注册100个用户用时 : " + limit + "秒";
	}

	@GetMapping(value = "config")
	public String getconfig(){

		//return ZMALLUSERSERVICE_URL;
		return userService.getconfig();
	}

	@GetMapping("/login")
	public ResponseEntity<?> login(@RequestParam("mobile") String mobile,@RequestParam("password") String password){
		Map<String,Object> user = new HashMap<String,Object>();
		user.put("userMobile",mobile);
		user.put("password",password);
		log.info("登录请求参数 : {} " , user);
		ResponseEntity<?> ret = userService.login(user);
		log.info("微服务login 返回值为: {} " , ret);
		return ret;
	}



}
